package com.cyt.gen.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.gen.dao.FormToolDao;
import com.cyt.gen.model.po.FormTool;
import com.cyt.gen.model.vo.FormToolVo;
import com.cyt.gen.service.FormToolService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 表单自定义-工具栏信息Service层实现类
 *
 * @author rong yuan
 * @date 2019-4-16 8:44:04
 */
@Service("formToolService")
public class FormToolServiceImpl extends BaseServiceImpl<FormTool> implements FormToolService {

    @Resource
    private FormToolDao formToolDao;

    @Autowired
    public void setBaseDao(FormToolDao formToolDao) {
        this.baseDao = formToolDao;
    }

    private Example initExample(FormToolVo formToolVo) {
        Example example = new Example(FormTool.class);
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(formToolVo.getTableName())) {
            criteria.andEqualTo("tableName", formToolVo.getTableName());
        }
        return example;
    }

    /**
     * 添加或更新
     *
     * @param formToolVo 参数接受对象
     * @param sysUser    当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-4-16 8:44:04
     */
    @Override
    public ReturnResult addOrUpdate(FormToolVo formToolVo, SysUser sysUser) {
        formToolDao.deleteByExample(initExample(formToolVo));
        FormTool formTool = new FormTool();
        formTool.setId(BaseUtil.uId());
        formTool.setTableName(formToolVo.getTableName());
        formTool.setAddFunction(formToolVo.getAddFunction());
        formTool.setEditFunction(formToolVo.getEditFunction());
        formTool.setDeleteFunction(formToolVo.getDeleteFunction());
        int num = formToolDao.insert(formTool);
        if (num <= 0) {
            return new ReturnResult(ErrorCode.CODE_1, ErrorCode.CODE_ADD_FAIL_MSG);
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_MSG);
    }

    /**
     * 查询对象集合
     *
     * @param formToolVo 对象
     * @return List 对象集合
     * @author rong yuan
     * @date 2019-4-16 8:44:04
     */
    @Override
    public List<FormTool> list(FormToolVo formToolVo) {
        return formToolDao.selectByExample(initExample(formToolVo));
    }
}